<?php namespace Modules\Shop\Http\Controllers;


class ArticleController extends Controller
{

    public function getIndex()
    {

        global $_CFG, $ecs, $err, $smarty, $_LANG, $db, $charset;

        /*------------------------------------------------------ */
        //-- INPUT
        /*------------------------------------------------------ */

        \Input::merge(['id' => is_set(\Input::get('id')) ? intval(\Input::get('id')) : 0]);
        $article_id = \Input::get('id');
        if (is_set(\Input::get('cat_id')) && \Input::get('cat_id') < 0) {
            $article_id = $db->getOne("SELECT article_id FROM " . $ecs->table('article') . " WHERE cat_id = '" . intval(\Input::get('cat_id')) . "' ");
        }

        /*------------------------------------------------------ */
        //-- PROCESSOR
        /*------------------------------------------------------ */

        $cache_id = sprintf('%X', crc32(\Input::get('id') . '-' . $_CFG['lang']));

        if (!\Tpl::is_cached('article.dwt', $cache_id)) {
            /* 文章详情 */
            $article = get_article_info($article_id);

            if (empty($article)) {
                ecs_header("Location: ./\n");
                exit;
            }

            if (!empty($article['link']) && $article['link'] != 'http://' && $article['link'] != 'https://') {
                ecs_header("location:$article[link]\n");
                exit;
            }

            \Tpl::assign('article_categories', article_categories_tree($article_id)); //文章分类树
            \Tpl::assign('categories', get_categories_tree());  // 分类树
            \Tpl::assign('helps', get_shop_help()); // 网店帮助
            \Tpl::assign('top_goods', get_top10());    // 销售排行
            \Tpl::assign('best_goods', get_recommend_goods('best'));       // 推荐商品
            \Tpl::assign('new_goods', get_recommend_goods('new'));        // 最新商品
            \Tpl::assign('hot_goods', get_recommend_goods('hot'));        // 热点文章
            \Tpl::assign('promotion_goods', get_promote_goods());    // 特价商品
            \Tpl::assign('related_goods', article_related_goods(\Input::get('id')));  // 特价商品
            \Tpl::assign('id', $article_id);
            \Tpl::assign('username', $_SESSION['user_name']);
            \Tpl::assign('email', $_SESSION['email']);
            \Tpl::assign('type', '1');
            \Tpl::assign('promotion_info', get_promotion_info());

            /* 验证码相关设置 */
            if ((intval($_CFG['captcha']) & CAPTCHA_COMMENT) && gd_version() > 0) {
                \Tpl::assign('enabled_captcha', 1);
                \Tpl::assign('rand', mt_rand());
            }

            \Tpl::assign('article', $article);
            \Tpl::assign('keywords', htmlspecialchars($article['keywords']));
            \Tpl::assign('description', htmlspecialchars($article['description']));

            $catlist = array();
            foreach (get_article_parent_cats($article['cat_id']) as $k => $v) {
                $catlist[] = $v['cat_id'];
            }

            assign_template('a', $catlist);

            $position = assign_ur_here($article['cat_id'], $article['title']);
            \Tpl::assign('page_title', $position['title']);    // 页面标题
            \Tpl::assign('ur_here', $position['ur_here']);  // 当前位置
            \Tpl::assign('comment_type', 1);

            /* 相关商品 */
            $sql = "SELECT a.goods_id, g.goods_name " .
                "FROM " . $ecs->table('goods_article') . " AS a, " . $ecs->table('goods') . " AS g " .
                "WHERE a.goods_id = g.goods_id " .
                "AND a.article_id = '\Input::get(id)' ";
            \Tpl::assign('goods_list', $db->getAll($sql));

            /* 上一篇下一篇文章 */
            $next_article = $db->getRow("SELECT article_id, title FROM " . $ecs->table('article') . " WHERE article_id > $article_id AND cat_id=$article[cat_id] AND is_open=1 LIMIT 1");
            if (!empty($next_article)) {
                $next_article['url'] = build_uri('article', array('aid' => $next_article['article_id']), $next_article['title']);
                \Tpl::assign('next_article', $next_article);
            }

            $prev_aid = $db->getOne("SELECT max(article_id) FROM " . $ecs->table('article') . " WHERE article_id < $article_id AND cat_id=$article[cat_id] AND is_open=1");
            if (!empty($prev_aid)) {
                $prev_article = $db->getRow("SELECT article_id, title FROM " . $ecs->table('article') . " WHERE article_id = $prev_aid");
                $prev_article['url'] = build_uri('article', array('aid' => $prev_article['article_id']), $prev_article['title']);
                \Tpl::assign('prev_article', $prev_article);
            }

            assign_dynamic('article');
        }
        if (isset($article) && $article['cat_id'] > 2) {
            \Tpl::display('article.dwt', $cache_id);
        } else {
            \Tpl::display('article_pro.dwt', $cache_id);
        }


        return view('shop::index');
    }

}

/*------------------------------------------------------
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 获得指定的文章的详细信息
 *
 * @access  private
 * @param   integer $article_id
 * @return  array
 */
function get_article_info($article_id)
{
    /* 获得文章的信息 */
    $sql = "SELECT a.*, IFNULL(AVG(r.comment_rank), 0) AS comment_rank " .
        "FROM " . \ECS::table('article') . " AS a " .
        "LEFT JOIN " . \ECS::table('comment') . " AS r ON r.id_value = a.article_id AND comment_type = 1 " .
        "WHERE a.is_open = 1 AND a.article_id = '$article_id' GROUP BY a.article_id";
    $row = \Ecdb::getRow($sql);

    if ($row !== false) {
        $row['comment_rank'] = ceil($row['comment_rank']);                              // 用户评论级别取整
        $row['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']); // 修正添加时间显示

        /* 作者信息如果为空，则用网站名称替换 */
        if (empty($row['author']) || $row['author'] == '_SHOPHELP') {
            $row['author'] = $GLOBALS['_CFG']['shop_name'];
        }
    }

    return $row;
}




/**
 * 获得文章关联的商品
 *
 * @access  public
 * @param   integer $id
 * @return  array
 */
function article_related_goods($id)
{
    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price AS org_price, ' .
        "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
        'g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date ' .
        'FROM ' . \ECS::table('goods_article') . ' ga ' .
        'LEFT JOIN ' . \ECS::table('goods') . ' AS g ON g.goods_id = ga.goods_id ' .
        "LEFT JOIN " . \ECS::table('member_price') . " AS mp ".
        "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
        "WHERE ga.article_id = '$id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0";
    $res = \Ecdb::query($sql);

    $arr = array();
    while ($row = \Ecdb::fetchRow($res))
    {
        $arr[$row['goods_id']]['goods_id']      = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']    = $row['goods_name'];
        $arr[$row['goods_id']]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
            sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        $arr[$row['goods_id']]['goods_thumb']   = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']     = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['market_price']  = price_format($row['market_price']);
        $arr[$row['goods_id']]['shop_price']    = price_format($row['shop_price']);
        $arr[$row['goods_id']]['url']           = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);

        if ($row['promote_price'] > 0)
        {
            $arr[$row['goods_id']]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
            $arr[$row['goods_id']]['formated_promote_price'] = price_format($arr[$row['goods_id']]['promote_price']);
        }
        else
        {
            $arr[$row['goods_id']]['promote_price'] = 0;
        }
    }

    return $arr;
}